package com.ztc.dao;

import com.ztc.beans.Glo;
import com.ztc.util.JDBCUtil;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

public class GloDao {

    public List<Glo> getsheng() {
       String sql = "select id,name,parentId,levelType from glo_area where levelType=1";
        Connection conn = null;
        PreparedStatement ps = null;
        ResultSet rs = null;
        List<Glo> glolist= new ArrayList<>();
        try {
            //连接数据库
            conn = JDBCUtil.getConnection();
            //创建执行sql的对象,预加载sql
            ps=conn.prepareStatement(sql);
            rs=ps.executeQuery();
            while (rs.next()){
                Glo glo = new Glo();
                glo.setId(rs.getInt("id"));
                glo.setName(rs.getString("name"));
                glo.setParentId(rs.getInt("parentId"));
                glo.setLevelType(rs.getInt("levelType"));
                glolist.add(glo);
            }
        }catch (SQLException e) {
            e.printStackTrace();
        }finally{
            //关闭连接
            JDBCUtil.close(conn,ps,rs);
        }
        return glolist;
    }

    public List<Glo> getshi(String id) {
        String sql = "select id,name,parentId,levelType from glo_area where parentId=?";
        Connection conn = null;
        PreparedStatement ps = null;
        ResultSet rs = null;
        List<Glo> glolist= new ArrayList<>();
        try {
            //连接数据库
            conn = JDBCUtil.getConnection();
            //创建执行sql的对象,预加载sql
            ps=conn.prepareStatement(sql);
            ps.setString(1,id);
            rs=ps.executeQuery();
            while (rs.next()){
                Glo glo = new Glo();
                glo.setId(rs.getInt("id"));
                glo.setName(rs.getString("name"));
                glo.setParentId(rs.getInt("parentId"));
                glo.setLevelType(rs.getInt("levelType"));
                glolist.add(glo);
            }
        }catch (SQLException e) {
            e.printStackTrace();
        }finally{
            //关闭连接
            JDBCUtil.close(conn,ps,rs);
        }
        return glolist;
    }
    //return null;
}
